package com.hzmg.akkacloud.workdemo.controller;

import com.google.common.collect.Maps;
import com.hzmg.akkacloud.common.model.WorkerInfo;
import com.hzmg.akkacloud.workdemo.service.SimpleService;
import com.hzmg.akkacloud.worker.background.ReactiveDiscoveryClient;
import com.hzmg.akkacloud.worker.common.utils.RemoteInvokeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@Controller
@ResponseBody
@Slf4j
public class demoController {
    @Resource
    SimpleService service;

    @PostMapping("/test")
    public Map<String, String> test(@RequestBody Map<String, String> maps) {
        log.info("test:" + maps.toString());
        return maps;
    }

    @GetMapping("/test1")
    @ResponseBody
    public String test1() {
        log.info("test1");
        Map<Integer, String> map = Maps.newHashMap();
        map.putIfAbsent(1, "test");
        service.simple(map);
        return "maps";
    }

    @GetMapping("/test2")
    @ResponseBody
    public Map<String, String> testService2() {
        log.info("test2");
        Map<String, String> map = Maps.newHashMap();
        map.put("hello", "world");
        return map;
    }

    @GetMapping("/test3")
    @ResponseBody
    public Map<String, String> testService3(@RequestParam("name") String name) {
        log.info(name);
        Map<String, String> map = Maps.newHashMap();
        map.put("hello", name);
        return map;
    }

    public static void test() {
        log.info("test run");
/*        Map<Integer, String> map = Maps.newHashMap();
        map.putIfAbsent(1, "test");
        RemoteInvokeUtils.getRemoteService(map, "simple", "akkcloud-client-demo");*/
/*            test();
            test2();
            test4();
            test3();
            test5();
            test6();*/

    }

    public static void test2() {
        log.info("test2 run");
        Map<Integer, String> map = Maps.newHashMap();
        map.putIfAbsent(1, "test");
        RemoteInvokeUtils.getRemoteService(map, "simple2", "akkcloud-client-demo");
    }

    public static void test3() {
        log.info("test3 run");
        WorkerInfo workerInfo = new WorkerInfo();
        workerInfo.setAkkaAddress("1111");
        WorkerInfo resultMap = RemoteInvokeUtils.getRemoteServiceResult(workerInfo, "simple3", "akkcloud-client-demo");
        log.info(resultMap.toString());
    }

    public static void test4() {
        log.info("test4 run");
        WorkerInfo workerInfo = ReactiveDiscoveryClient.getAllAliveWorkers("akkcloud-client-demo").get(0);
        RemoteInvokeUtils.getRemoteService(workerInfo, "simple4", "akkcloud-client-demo");
    }

    public static void test5() {
        log.info("test5 run");
        RemoteInvokeUtils.getRemoteService(null, "simple5", "akkcloud-client-demo");
    }

    public static void test6() {
        log.info("test6 run");
        WorkerInfo workerInfo = RemoteInvokeUtils.getRemoteServiceResult(null, "simple6", "akkcloud-client-demo");
        log.info(workerInfo.toString());
    }
}
